package com.longxi.tianjintianbao.utils;

import com.longxi.tianjintianbao.api.response.HttpResult;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.LazyFragment;
import com.longxi.tianjintianbao.widget.EmptyLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;

public class NListRefresh<T> {

    private static NListRefresh mInstance;

    public static NListRefresh getInstance(){
        if(mInstance == null){
            synchronized (NListRefresh.class){
                if(mInstance == null){
                    mInstance = new NListRefresh();
                }
            }
        }
        return mInstance;
    }

    public Items fjData(HttpResult<T> result,
                        SmartRefreshLayout smartRefreshLayout,
                        Items items,
                        MultiTypeAdapter adapter,
                        LazyFragment fragment) {
        if (result.getCode() != 0) {
            if(smartRefreshLayout.isRefreshing()){
                smartRefreshLayout.finishRefresh(false);//传入false表示刷新失败
            }
            if(smartRefreshLayout.isLoading()){
                smartRefreshLayout.finishLoadMore(false);//传入false表示加载失败
            }
            MyUtils.showToast(result.getMsg());
            EmptyLayout.ERROR_TIP = result.getMsg() + "错误代码:" + result.getCode();
            fragment.showError();
        } else {
            if(smartRefreshLayout.isRefreshing()){
                items = new Items();
                smartRefreshLayout.finishRefresh();//传入false表示刷新失败
            }
            if(smartRefreshLayout.isLoading()){
                smartRefreshLayout.finishLoadMore();//传入false表示加载失败
            }
            if(items == null){
                items = new Items();
            }

            if(items.size() == 0 && result.getData() == null ){
                items = new Items();
                adapter.setItems(items);
                adapter.notifyDataSetChanged();
                fragment.showNoData();
                return items;
            }

            if(items.size() == 0 && result.getData().size() == 0 ){
                items = new Items();
                adapter.setItems(items);
                adapter.notifyDataSetChanged();
                fragment.showNoData();
            }else if(result.getData().size() ==0){
                MyUtils.showToast("没有更多数据");
            }else{
                fragment.hideLoading();
                items.addAll(result.getData());
                adapter.setItems(items);
                adapter.notifyDataSetChanged();
                return items;
            }
        }

        return items;
    }

    public Items fjData(HttpResult<T> result,
                        SmartRefreshLayout smartRefreshLayout,
                        Items items,
                        MultiTypeAdapter adapter,
                        BaseActivity content) {
        if (result.getCode() != 0) {
            if(smartRefreshLayout.isRefreshing()){
                smartRefreshLayout.finishRefresh(false);//传入false表示刷新失败
            }
            if(smartRefreshLayout.isLoading()){
                smartRefreshLayout.finishLoadMore(false);//传入false表示加载失败
            }
            MyUtils.showToast(result.getMsg());
            if(result.getCode() == 104){
                content.showNoData();
            }else {
                EmptyLayout.ERROR_TIP = result.getMsg() + "错误代码:" + result.getCode();
                content.showError();
            }
        } else {
            if(smartRefreshLayout.isRefreshing()){
                items = new Items();
                smartRefreshLayout.finishRefresh();//传入false表示刷新失败
            }
            if(smartRefreshLayout.isLoading()){
                smartRefreshLayout.finishLoadMore();//传入false表示加载失败
            }
            if(items == null){
                items = new Items();
            }
            if(items.size() == 0 && result.getData().size() == 0 ){
                items = new Items();
                adapter.setItems(items);
                adapter.notifyDataSetChanged();
                content.showNoData();
            }else if(result.getData().size() ==0){
                MyUtils.showToast("没有更多数据");
            }else{
                content.hideLoading();
                items.addAll(result.getData());
                adapter.setItems(items);
                adapter.notifyDataSetChanged();
                return items;
            }
        }

        return items;
    }
}
